/**
 * Copyright Dinglian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lyh.provider.controller;

/**
 * 类描述
 *
 * @author LinYeHui
 * @date 2022/5/26
 * @company Dinglian
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

/**定义Controller对象(这个对象在spring mvc中给他的定义是handler),
 * 基于此对象处理客户端的请求*/
@RestController
public class ProviderController{
    //@Value默认读取项目配置文件中配置的内容
    //8080为没有读到server.port的值时,给定的默认值

    @Value("${server.port:8080}")
    private String server;
    //http://localhost:8081/provider/echo/tedu
    @GetMapping("/provider/echo/{msg}")
    public String doRestEcho1(@PathVariable String msg){
        return server+" say hello "+msg;
    }

    /**
     *演示慢调用过程下的限流
     **/
    //AtomicLong 类支持线程安全的自增自减操作
    private AtomicLong atomicLong=new AtomicLong(1);
    @GetMapping("/sentinel05")
    public  String doSentinel05() throws InterruptedException {
        //获取自增对象的值,然后再加1
        long num=atomicLong.getAndIncrement();
        if(num%2==0){//模拟50%的慢调用比例
            Thread.sleep(200);
        }
        return "sentinel 04 test";
    }
}
